from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _


class User(AbstractUser):
    """扩展用户模型"""
    
    class UserType(models.TextChoices):
        CUSTOMER = 'customer', _('普通用户')
        MERCHANT = 'merchant', _('商家')
        ADMIN = 'admin', _('管理员')
    
    class Gender(models.TextChoices):
        MALE = 'M', _('男')
        FEMALE = 'F', _('女')
        OTHER = 'O', _('其他')
        SECRET = 'S', _('保密')
    
    email = models.EmailField(_('邮箱地址'), unique=True)
    phone = models.CharField(_('手机号码'), max_length=11, blank=True)
    avatar = models.ImageField(_('头像'), upload_to='avatars/', blank=True, null=True)
    user_type = models.CharField(_('用户类型'), max_length=10, choices=UserType.choices, default=UserType.CUSTOMER)
    gender = models.CharField(_('性别'), max_length=1, choices=Gender.choices, default=Gender.SECRET)
    birth_date = models.DateField(_('生日'), blank=True, null=True)
    is_verified = models.BooleanField(_('是否验证'), default=False)
    is_vip = models.BooleanField(_('VIP会员'), default=False)
    vip_expire_date = models.DateTimeField(_('VIP到期时间'), blank=True, null=True)
    balance = models.DecimalField(_('账户余额'), max_digits=10, decimal_places=2, default=0.00)
    points = models.IntegerField(_('积分'), default=0)
    created_at = models.DateTimeField(_('创建时间'), auto_now_add=True)
    updated_at = models.DateTimeField(_('更新时间'), auto_now=True)
    
    class Meta:
        db_table = 'users'
        verbose_name = _('用户')
        verbose_name_plural = _('用户')
    
    def __str__(self):
        return self.username


class Address(models.Model):
    """用户收货地址"""
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='addresses', verbose_name=_('用户'))
    name = models.CharField(_('收货人姓名'), max_length=50)
    phone = models.CharField(_('收货人电话'), max_length=11)
    province = models.CharField(_('省份'), max_length=50)
    city = models.CharField(_('城市'), max_length=50)
    district = models.CharField(_('区县'), max_length=50)
    detail = models.CharField(_('详细地址'), max_length=200)
    postal_code = models.CharField(_('邮政编码'), max_length=6, blank=True)
    is_default = models.BooleanField(_('是否默认地址'), default=False)
    created_at = models.DateTimeField(_('创建时间'), auto_now_add=True)
    updated_at = models.DateTimeField(_('更新时间'), auto_now=True)
    
    class Meta:
        db_table = 'addresses'
        verbose_name = _('收货地址')
        verbose_name_plural = _('收货地址')
    
    def __str__(self):
        return f"{self.name} - {self.province}{self.city}{self.district}"


class MerchantProfile(models.Model):
    """商家资料"""
    
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='merchant_profile', verbose_name=_('用户'))
    business_name = models.CharField(_('店铺名称'), max_length=100)
    business_license = models.CharField(_('营业执照号'), max_length=50, unique=True)
    business_address = models.CharField(_('营业地址'), max_length=200)
    contact_person = models.CharField(_('联系人'), max_length=50)
    contact_phone = models.CharField(_('联系电话'), max_length=11)
    description = models.TextField(_('店铺描述'), blank=True)
    logo = models.ImageField(_('店铺logo'), upload_to='merchant_logos/', blank=True, null=True)
    is_approved = models.BooleanField(_('是否审核通过'), default=False)
    commission_rate = models.DecimalField(_('佣金比例'), max_digits=5, decimal_places=2, default=5.00)
    created_at = models.DateTimeField(_('创建时间'), auto_now_add=True)
    updated_at = models.DateTimeField(_('更新时间'), auto_now=True)
    
    class Meta:
        db_table = 'merchant_profiles'
        verbose_name = _('商家资料')
        verbose_name_plural = _('商家资料')
    
    def __str__(self):
        return self.business_name
